/*前台用户相关*/

$(function() {
	//验证码控件
	var myCaptcha = _dx.Captcha(document.getElementById('c1'), {
		appId : 'df3dab759731a977f2c10924f9412b6c', //appId,开通服务后可在控制台中“服务管理”模块获取
		width : 229,
		success : function(token) {
			// console.log('token:', token)
			$("#token").val(token);
		}
	});

	//为注册表单中所有input标签绑定blur事件
	$("#regform input").each(function(index, element) {
		$(element).blur(function() {
			$("#regmsg").html("");
//			$("#ttmsg").html("");
//			$("#tcmsg").html("");
		});
	});

	//验证密码长度
	$("#password").blur(function() {
		$("#ttmsg").html("");
		var pwd = $("#password").val();
		if($.trim(pwd).length<6 ){
			$("#ttmsg").html("密码至少6位！");
		}
	});

	//为移动电话标签绑定blur事件
	$("#cellphone").blur(function(){
		$("#tcmsg").html("");
		var cphone = $("#cellphone").val();
		var pattern = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;

		if($.trim(cphone) != ""){
			if (!pattern.test(cphone)) {
				$("#tcmsg").html("移动电话格式错误！");
			}else{
				/* Ajax异步请求 */
				$.ajax({
					type : "POST",
					url : "/E-Shop/user/checkCellphone.do",
					dataType:"json",
					data : "cellphone="+ cphone,
					beforeSend : function() {
						$("#ajaximg1").show();
					},
					success : function(obj) {
						if(obj.state=="0"){
							$("#tcmsg").html(obj.message).css("color","red");
						}else if(obj.state=="1") {
							$("#tcmsg").html(obj.message+"<span style='font-size:20px;'>√</span>").css("color","green");
						}else{
							$("#tcmsg").html(obj.message+"<span style='font-size:20px;'>x</span>").css("color","red");
						}
					},
					error : function() {
						$("#ajaximg1").hide();
					},
					complete : function() {
						$("#ajaximg1").hide();
					}
				});				
			}
		}
	});

	//为email标签绑定blur事件
	$("#email").blur(function() {
		$("#tpmsg").html("");		
		var emailValue = $(this).val();
		var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$/;

		if ($.trim(emailValue) != "") {
			if (!pattern.test(emailValue)) {
				$("#tpmsg").html("邮箱格式错误！");
				$("#tpmsg").css("color","red");
			} else {												
				/* Ajax异步请求 */
				$.ajax({
					type : "POST",
					url : "/E-Shop/user/checkEmail.do",
					dataType:"json",
					data : "email="+ emailValue,
					beforeSend : function() {
						$("#ajaximg").show();
					},
					success : function(obj) {
						if(obj.state=="0"){
							$("#tpmsg").html(obj.message).css("color","red");
						}else if(obj.state=="1") {
							$("#tpmsg").html(obj.message+"<span style='font-size:20px;'>√</span>").css("color","green");
						}else{
							$("#tpmsg").html(obj.message+"<span style='font-size:20px;'>x</span>").css("color","red");
						}
					},
					error : function() {
						$("#ajaximg").hide();
					},
					complete : function() {
						$("#ajaximg").hide();
					}
				});
			}
		}
	});
})
